package com.elmcode.server.controller.app;

import com.elmcode.common.utils.PageUtils;
import com.elmcode.common.utils.R;
import com.elmcode.server.dto.AddressDTO;
import com.elmcode.server.dto.EditAddressDTO;
import com.elmcode.server.entity.AddressEntity;
import com.elmcode.server.service.AddressService;
import com.elmcode.server.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 收货地址
 *
 * @author laozj
 * @email laozj@elm.com
 * @date 2024-06-13 13:05:04
 */
@Api(tags = "地址接口")
@RestController
@RequestMapping("app/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("查询地址接口")
    public Result list(@RequestParam Map<String, Object> params){
        PageUtils page = addressService.queryPage(params);

        return Result.ok("查询成功",page.getList(),page.getTotal());
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    public Result info(@PathVariable("id") Long id){
		AddressEntity address = addressService.getById(id);

        return Result.ok("查找成功",address);
    }

    /**
     * 添加地址接口
     */
    @PostMapping
    @ApiOperation("添加地址接口")
    public Result save(AddressDTO address){
		addressService.saveAddress(address);

        return Result.ok("添加成功");
    }

    /**
     * 修改地址接口
     */
    @PutMapping("/{id}")
    @ApiOperation("更新地址接口")
    public Result update(@PathVariable("id")Long id, EditAddressDTO address){
		addressService.updateAddress(id,address);

        return Result.ok("修改成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除地址接口")
    public Result delete(@PathVariable("id") Long id){
		addressService.removeById(id);

        return Result.ok("删除成功");
    }


    /**
     * 获取默认地址
     */
    @GetMapping("/default")
    @ApiOperation("删除地址接口")
    public Result defaultAddr(){
        AddressEntity address = addressService.getDefaultAddr();

        return Result.ok("删除成功",address);
    }

}
